//
//  MSTradeCell.m
//  SocietySale
//
//  Created by Mike on 16/11/7.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSTradeCell.h"

@interface MSTradeCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UILabel *tradeNumLabel;
@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UILabel *themeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *commentLabel;

@property (nonatomic, strong) UILabel *tradePriceLabel;
@property (nonatomic, strong) UILabel *memberTypeLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView  *bgView;
@property (nonatomic, strong) UIView *shortHorizonLineView;
@end

@implementation MSTradeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.titleLabel = [self creatLabel:CGRectMake(12, 8, SCREEN_WIDTH, 25)];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textColor = [UIColor blackColor];
    
    self.typeLabel = [self creatLabel:CGRectMake(12, self.titleLabel.bottom+5, SCREEN_WIDTH -140, 25)];
    self.stateLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH -140, self.typeLabel.top+2.5, 60, 20)];
    self.stateLabel.textAlignment = NSTextAlignmentCenter;

    self.tradeNumLabel = [self creatLabel:CGRectMake(self.stateLabel.right+8, self.typeLabel.top, 60, 25)];
    self.tradeNumLabel.textAlignment = NSTextAlignmentRight;
    
    self.nameLabel = [self creatLabel:CGRectMake(12, self.typeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    
    self.themeLabel = [self creatLabel:CGRectMake(12, self.nameLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.contentLabel = [self creatLabel:CGRectMake(12, self.themeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.commentLabel = [self creatLabel:CGRectMake(12, self.contentLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    
//    NSLog(@"评价底部：%f",self.commentLabel.bottom); //183   198
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.commentLabel.bottom + 15 + 20 + 29, SCREEN_WIDTH, 8)]; // top:227
    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bgView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView];
    
    self.tradePriceLabel = [self creatLabel:CGRectMake(12, _bgView.top-49, SCREEN_WIDTH/2, 20)];
    self.memberTypeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH/2+5, self.tradePriceLabel.top, 130, 20)];
    self.timeLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH-92, self.tradePriceLabel.top, 80, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;

    _shortHorizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, _bgView.top-37, SCREEN_WIDTH-24, 0.6)];// 183+7 190
    _shortHorizonLineView.backgroundColor = kNormalGray;
    [self.contentView addSubview:_shortHorizonLineView];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

- (void)setModel:(MSTradeModel *)model{
    _model = model;
    self.titleLabel.text = model.title;
    self.typeLabel.attributedText = [self getMutableAttriStrWithForStr:@"类型   " backStreet:model.projectType];//;
    self.stateLabel.text = model.tradeState;
    self.tradeNumLabel.text = [NSString stringWithFormat:@"%@次交易",model.tradeNum];
    
    CGSize typeSize = [HDUtil getSizeWithText:[NSString stringWithFormat:@"类型   %@",model.projectType] font:HeitiFont(14)];
    self.stateLabel.left = MIN(typeSize.width + 24, SCREEN_WIDTH -135);
    
    if ([model.tradeState isEqualToString:@"1"]) {
        self.stateLabel.text = @"交易中";
        self.stateLabel.textColor = UIColorFromRGB(0x3bb25b);
        self.stateLabel.backgroundColor = UIColorHexA(0x3bb25b, 0.1);
    }
    if ([model.tradeState isEqualToString:@"2"]) {
        self.stateLabel.text = @"公开显示";
        self.stateLabel.textColor = kBlueColor;
        self.stateLabel.backgroundColor = UIColorHexA(0x3b99dc, 0.1);
    }
    if ([model.tradeState isEqualToString:@"3"]) {
        self.stateLabel.text = @"已过期";
        self.stateLabel.textColor = kDarkGrayColor;
        self.stateLabel.backgroundColor = kDefaultViewBackgroundColor;
    }
    CGSize stateSize = [HDUtil getSizeWithText:self.stateLabel.text font:HeitiFont(14)];
    self.stateLabel.width = stateSize.width + 10;
    self.tradeNumLabel.left = self.stateLabel.right + 4;

    NSString * haveName = model.name.length?@"y":@"n";
    NSString * haveTheme = model.theme.length?@"y":@"n";
    NSString * haveContent = model.content.length?@"y":@"n";
    NSString * haveTradeEvaluate = [model.tradeEvaluate integerValue]>0?@"y":@"n";
    
    NSMutableArray *haveArray = [NSMutableArray arrayWithCapacity:0];
    NSArray *stateArray = @[haveName,haveTheme,haveContent,haveTradeEvaluate];
    for (NSString *stateStr in stateArray) {
        if ([stateStr isEqualToString:@"y"]) {
            [haveArray addObject:stateStr];
        }
    }
    float height = 64+haveArray.count*30+44+13;
    
    _shortHorizonLineView.top = height-50;
    _bgView.top = height-8;
    self.tradePriceLabel.top = _shortHorizonLineView.top + 10;
    self.memberTypeLabel.top = self.tradePriceLabel.top;
    self.timeLabel.top = self.tradePriceLabel.top;

    if (model.name.length) {//有姓名
        self.nameLabel.hidden = NO;
        self.nameLabel.top = self.typeLabel.bottom + 5;
        if (model.theme.length) {//有主题
            self.themeLabel.hidden = NO;
            self.themeLabel.top = self.nameLabel.bottom + 5;
        }
        else{//无主题
            self.themeLabel.hidden = YES;
        }
        self.nameLabel.attributedText = [self getMutableAttriStrWithForStr:@"姓名   " backStreet:model.name];
        if (model.theme.length) {
            self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"职务   " backStreet:model.position];
        }
    }
    else{//无姓名
        self.nameLabel.hidden = YES;
        if (model.theme.length) {//有主题
            self.themeLabel.hidden = NO;
            self.themeLabel.top = self.typeLabel.bottom + 5;
        }
        else{//无主题
            self.themeLabel.hidden = YES;
        }
        if (model.theme.length) {
            self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"主题   " backStreet:model.theme];
        }
    }
    
//    if (model.theme.length) {
//        self.themeLabel.attributedText = [self getMutableAttriStrWithForStr:@"主题   " backStreet:model.theme];
//    }
    if (model.content.length) {
        self.contentLabel.attributedText = [self getMutableAttriStrWithForStr:@"内容   " backStreet:model.content];
    }
    
    if ([model.tradeEvaluate integerValue]>0) {//有评价
        self.commentLabel.hidden = NO;
        self.commentLabel.top = _shortHorizonLineView.top-33;
        if (model.content.length) {//有内容
            self.contentLabel.hidden = NO;
            self.contentLabel.top = self.commentLabel.top-30;
        }
        else{//无内容
            self.contentLabel.hidden = YES;
        }
        NSMutableAttributedString *commentAttstr = [[NSMutableAttributedString alloc] initWithString:@"评价                         "];
        
        NSTextAttachment *textAttachment0 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment0.bounds = CGRectMake(0, 0, 10, 10);
        textAttachment0.image = [UIImage imageNamed:@"star1"];//亮的
        NSAttributedString *attachmentString0 = [NSAttributedString attributedStringWithAttachment:textAttachment0];
        
        NSTextAttachment *textAttachment3 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment3.bounds = CGRectMake(0, 0, 10, 10);
        textAttachment3.image = [UIImage imageNamed:@"star2"];//灰的
        NSAttributedString *attachmentString3 = [NSAttributedString attributedStringWithAttachment:textAttachment3];
        
        int startNum = MIN(4, [model.tradeEvaluate intValue]);
        for (int i = 0; i<startNum; i++) {
            [commentAttstr insertAttributedString:attachmentString0 atIndex:5+i*2];
        }
        for (int i = startNum; i<4; i++) {
            [commentAttstr insertAttributedString:attachmentString3 atIndex:5+i*2];
        }
        
//        [commentAttstr insertAttributedString:attachmentString0 atIndex:5];
//        [commentAttstr insertAttributedString:attachmentString0 atIndex:7];
//        [commentAttstr insertAttributedString:attachmentString0 atIndex:9];
//        [commentAttstr insertAttributedString:attachmentString3 atIndex:11];
        
        self.commentLabel.attributedText = commentAttstr;
    }
    else{//无评价
        self.commentLabel.hidden = YES;
        if (model.content.length) {//有内容
            self.contentLabel.hidden = NO;
            self.contentLabel.top = _shortHorizonLineView.top-33;
        }
        else{//无内容
            self.contentLabel.hidden = YES;
        }
    }
    
    CGSize priceSize = [HDUtil getSizeWithText:model.tradePrice font:[UIFont systemFontOfSize:15]];
    self.memberTypeLabel.left = 115+priceSize.width;
    
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"交易价格     x" attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:8];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:model.tradePrice attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]}];
    [priceAttstr appendAttributedString:priStr];
    self.tradePriceLabel.attributedText = priceAttstr;
    
    NSString *memberTypeStr = @"";
    if (model.isSelf) {
        memberTypeStr = @"[我]";
    }
    else{
        memberTypeStr = [NSString stringWithFormat:@"[%@]",model.memberType];
    }
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:memberTypeStr attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    if ([model.memberLevel integerValue] >0) {
        NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
        memberAttachment.image = [UIImage imageNamed:@"diamond"];
        NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
        
        for (int i = 0; i<[model.memberLevel integerValue]; i++) {
            [memberAttstr insertAttributedString:memberAttachmentStr atIndex:memberTypeStr.length+i];
        }
    }
    self.memberTypeLabel.attributedText = memberAttstr;
    
    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
}

- (NSMutableAttributedString*)getMutableAttriStrWithForStr:(NSString*)forStr backStreet:(NSString*)backStr{
    NSMutableAttributedString *AttStr = [[NSMutableAttributedString alloc] initWithString:forStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSAttributedString *backAttStr = [[NSAttributedString alloc] initWithString:backStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:[UIColor blackColor]}];
    [AttStr appendAttributedString:backAttStr];
    
    return AttStr;
}

+ (CGFloat)cellHeightWithModel:(MSTradeModel*)model{//  +72
    NSString * haveName = model.name.length?@"y":@"n";
    NSString * haveTheme = model.theme.length?@"y":@"n";
    NSString * haveContent = model.content.length?@"y":@"n";
    NSString * haveTradeEvaluate = [model.tradeEvaluate integerValue]>0?@"y":@"n";
    
    NSMutableArray *haveArray = [NSMutableArray arrayWithCapacity:0];
    NSArray *stateArray = @[haveName,haveTheme,haveContent,haveTradeEvaluate];
    for (NSString *stateStr in stateArray) {
        if ([stateStr isEqualToString:@"y"]) {
            [haveArray addObject:stateStr];
        }
    }
    float height = 64+haveArray.count*30+44+13;
    
    return height;//235
}

@end
